package cn.source.controller;
import cn.source.common.annotation.Log;
import cn.source.common.core.controller.BaseController;
import cn.source.common.core.domain.AjaxResult;
import cn.source.common.core.page.TableDataInfo;
import cn.source.common.enums.BusinessType;
import cn.source.domain.ArtInfor;
import cn.source.domain.PartyHeartfelt;
import cn.source.domain.WarmHeartStation;
import cn.source.mapper.ArtInforMapper;
import cn.source.service.ArtInforService;
import cn.source.service.IArtStartService;
import cn.source.service.IPartyHeartfeltService;
import cn.source.service.IWarmHeartStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 党建移动端接口控制器
 */
@RestController
@RequestMapping("/api")
public class PartyBuildingMobileController extends BaseController {

    @Autowired
    private ArtInforService artInforService;
    @Autowired 
    private ArtInforMapper artInforMapper;
    @Autowired
    private IPartyHeartfeltService partyHeartfeltService;
    @Autowired
    private IWarmHeartStationService heartStationService;
    @Autowired
    private IArtStartService artStartService;

    /**
     * 获取收藏文章详细信息
     */
    @GetMapping("/start/{id}/{userId}")
    public AjaxResult getStartArticle(@PathVariable("id") Long id, @PathVariable("userId") Long userId) {
        return AjaxResult.success(artStartService.selectArtStartById(id, userId));
    }

    /**
     * 查询暖心驿站信息列表
     */
    @GetMapping("/station/list") 
    public TableDataInfo listWarmHeartStation(WarmHeartStation warmHeartStation) {
        startPage();
        List<WarmHeartStation> list = heartStationService.selectWarmHeartStationList(warmHeartStation);
        return getDataTable(list);
    }

    /**
     * 获取暖心驿站信息详细信息
     */
    @GetMapping("/station/{id}")
    public AjaxResult getWarmHeartStation(@PathVariable("id") Long id) {
        return AjaxResult.success(heartStationService.selectWarmHeartStationById(id));
    }

    /**
     * 查询心愿列表
     */
    @GetMapping("/artApi/partyHeartfelt")
    public ResponseEntity<List<PartyHeartfelt>> getPartyHeartfeltByUserId(
            @RequestParam Long userId,
            @RequestParam Long heartfeltTypeInt) {
        try {
            List<PartyHeartfelt> partyHeartfelts = partyHeartfeltService.selectPartyHeartfeltByUserId(userId, heartfeltTypeInt);
            return ResponseEntity.ok(partyHeartfelts);
        } catch (Exception e) {
            logger.error("查询心愿列表失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     * 新增心愿
     */
    @Log(title = "用户新增心愿", businessType = BusinessType.INSERT)
    @PostMapping("/artApi/partyHeartfelt")
    public AjaxResult addPartyHeartfelt(@RequestBody PartyHeartfelt partyHeartfelt) {
        try {
            return partyHeartfeltService.insertPartyHeartfelt(partyHeartfelt) > 0 
                ? AjaxResult.success("心愿新增成功")
                : AjaxResult.error("心愿新增失败");
        } catch (Exception e) {
            logger.error("新增心愿失败", e);
            return AjaxResult.error("系统错误，请稍后重试");
        }
    }

    /**
     * 查询文章列表
     */
    @GetMapping("/artApi/list")
    public TableDataInfo listArticles(ArtInfor artInfor) {
        startPage();
        List<ArtInfor> list = artInforService.selectArtInforList(artInfor);
        return getDataTable(list);
    }

    /**
     * 查询收藏文章列表
     */
    @GetMapping("/artApi/startList/{userId}")
    public TableDataInfo listStartArticles(@PathVariable("userId") Long userId) {
        startPage();
        List<ArtInfor> list = artInforMapper.ListStartArtInforByUserId(userId);
        return getDataTable(list);
    }

    /**
     * 获取文章详细信息
     */
    @GetMapping("/artApi/{id}")
    public AjaxResult getArticleInfo(@PathVariable("id") Long id) {
        ArtInfor artInfor = artInforMapper.selectArtInforContentById(id);
        return AjaxResult.success(artInfor);
    }

}
